package com.rwx.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.rwx.constant.MessageConstant;
import com.rwx.constant.RedisConstant;
import com.rwx.entity.PageResult;
import com.rwx.entity.QueryPageBean;
import com.rwx.entity.Result;
import com.rwx.pojo.Setmeal;
import com.rwx.service.SetmealService;
import com.rwx.util.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;


    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){

        try {
            String originalFilename = imgFile.getOriginalFilename();
            String extention = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName= UUID.randomUUID()+extention;
            String filePath = AliOssUtil.upload(imgFile.getBytes(), objectName);
            //将上传图片名称存入Redis，基于Redis的Set集合存储
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,objectName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,filePath);
        } catch (IOException e) {
            //log.error("文件上传失败");
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);

        }


    }

    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try {
            setmealService.add(setmeal,checkgroupIds);
        }catch (Exception e){
            //新增套餐失败
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }
        //新增套餐成功
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }






















}
